package com.luoyi.util.ehr;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 字典json字符转成枚举类
 *
 * @author xt
 */
public class DictValueToEnumClass {

    /**
     * 模板
     */
    final private static String TEMP_TEXT =
            "import lombok.AllArgsConstructor;\n" +
            "import lombok.Getter;\n" +
            "\n" +
            "/**\n" +
            " * {classDesc}\n" +
            " *\n" +
            " * @author xt\n" +
            " */\n" +
            "@Getter\n" +
            "@AllArgsConstructor\n" +
            "public enum {ClassName} {\n" +
            "\n" +
            "{field}\n" +
            "    ;\n" +
            "    final private String key;\n" +
            "    final private String value;\n" +
            "}";


    public static void main(String[] args) {
        String text = "[{\"KEY\":\"1\",\"VALUE\":\"1 独立董事\"},{\"KEY\":\"A\",\"VALUE\":\"A 合同用工\"},{\"KEY\":\"B\",\"VALUE\":\"B 试用期合同工\"},{\"KEY\":\"C\",\"VALUE\":\"C 派遣员工\"},{\"KEY\":\"D\",\"VALUE\":\"D 实习\"},{\"KEY\":\"E\",\"VALUE\":\"E 返聘\"},{\"KEY\":\"F\",\"VALUE\":\"F 湖机待岗\"},{\"KEY\":\"G\",\"VALUE\":\"G 挂靠\"},{\"KEY\":\"H\",\"VALUE\":\"H 挂职\"},{\"KEY\":\"I\",\"VALUE\":\"I 兼职\"},{\"KEY\":\"J\",\"VALUE\":\"J 委派(总部派入人员)\"},{\"KEY\":\"Z\",\"VALUE\":\"Z 上线前已离职人员\"},{\"KEY\":\"\",\"VALUE\":\"　　\"}]";
        execute(text, "EmployeeGroupEnum", "员工组");
    }

    public static void execute(String text, String className, String classDesc) {
        JSONArray objects = JSONArray.parseArray(text);
        StringBuilder fieldSb = new StringBuilder();
        for (int i = 0; i < objects.size(); i++) {
            JSONObject object = objects.getJSONObject(i);
            String key = object.getString("KEY");
            String value = object.getString("VALUE");
            if (StrUtil.isBlank(key)) {
                continue;
            }
            fieldSb.append(StrUtil.format("    /**\n" +
                    "     * {}\n" +
                    "     */\n", value));
            fieldSb.append(StrUtil.format("    {}(\"{}\", \"{}\"),\n", "KEY" + key, key, value));
        }
        Map<String, String> objectObjectHashMap = new HashMap<>(2);
        objectObjectHashMap.put("ClassName", className);
        objectObjectHashMap.put("field", fieldSb.toString());
        objectObjectHashMap.put("classDesc", classDesc);
        String format = StrUtil.format(TEMP_TEXT, objectObjectHashMap);
        System.out.println(format);
    }



}
